<template>
  <!-- 添加机构成员 -->
  <el-dialog
    top="30vh"
    :title="$t('members.chengyuan53')"
    @close="cancleHandle"
    :visible.sync="dialogShow"
    width="561px"
    height="262px"
    :close-on-click-modal="false"
  >
    <div class="file_change_inner">
      <el-form
        ref="form"
        label-position="top"
        label-width="100px"
        :model="form"
        :rules="rules"
        :size="formItemSize"
      >
        <el-form-item :label="$t('members.chengyuan54')" prop="phoneOrEmail">
          <el-input
            v-model="form.phoneOrEmail"
            style="width: 320px"
            :placeholder="$t('members.chengyuan55')"
          />
        </el-form-item>
        <el-form-item :label="$t('members.chengyuan56')" prop="roleId">
          <el-radio v-model="form.roleId" size="mini" label="1">
            {{ $t('members.chengyuan57') }}
          </el-radio>
          <el-radio v-if="user_owner == 1" v-model="form.roleId" :size="formItemSize" label="2">{{
            $t('members.chengyuan58')
          }}</el-radio>
        </el-form-item>
      </el-form>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button plain :size="optionButtonSize" @click="cancleHandle">{{
        $t('members.chengyuan59')
      }}</el-button>
      <el-button type="primary" :size="optionButtonSize" @click="submitHandle">{{
        $t('members.chengyuan60')
      }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'BLZMasterIndex',

  data() {
    return {
      dialogShow: false,
      form: {
        phoneOrEmail: '',
        roleId: '',
      },
      rules: {
        phoneOrEmail: [
          { required: true, message: this.$t('members.chengyuan61'), trigger: 'blur' },
        ],
        roleId: [
          {
            required: true,
            message: this.$t('members.chengyuan62'),
            trigger: 'change',
          },
        ],
      },
      user_owner: '',
    };
  },

  mounted() {
    this.user_owner = localStorage.getItem('user_owner');
  },

  methods: {
    cancleHandle() {
      this.dialogShow = false;
      this.form = {
        phoneOrEmail: '',
        roleId: '',
      };
    },
    submitHandle() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const organizedType = localStorage.getItem('user_organizedType'); // 1诊所  2技工所
          if (this.form.roleId == 1) {
            if (organizedType == 1) {
              this.form.roleId = 3;
            } else if (organizedType == 2) {
              this.form.roleId = 5;
            }
          } else if (this.form.roleId == 2) {
            if (organizedType == 1) {
              this.form.roleId = 2;
            } else if (organizedType == 2) {
              this.form.roleId = 4;
            }
          }
          this.$http
            .post('/sys/organized/addMember', this.form)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              } else {
                this.$message.success(this.$t('members.chengyuan63'));
                this.$emit('getMemberList');
                this.dialogShow = false;
                this.form = {
                  phoneOrEmail: '',
                  roleId: '',
                };
              }
            })
            .catch((err) => {
              console.log('err', err);
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
